﻿Imports System.Data.OleDb
Public Class frmChangePassword
    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Dim encrypt As New Common
        Dim ds As New DataSet
        If txtNewPassword.Text = "" Then
            lblError.Text = "(*)Password mới chưa được nhập"
            Exit Sub
        End If
        If txtNewPassword.Text <> txtVerifyPassword.Text Then
            lblError.Text = "(*)Xác nhận lại Password không chính xác"
            Exit Sub
        End If
        Dim query As String = "Select password  from Users where username='" & frmMain.UserName & "'"
        Dim dataAccess As New DataAccess
        Dim dbConn As New OleDb.OleDbConnection
        Dim dbCommand As OleDbCommand
        Dim oldPassword As String
        dataAccess.ConnectDatabase_DBAccess(dbConn)
        dbCommand = New OleDbCommand(query, dbConn)
        oldPassword = dbCommand.ExecuteScalar()
        If oldPassword <> encrypt.Encrypt(txtOldPassword.Text) Then
            lblError.Text = "(*)Password cũ bạn nhập không chính xác"
            dbConn.Close()
            Exit Sub
        End If
        'Change Password
        Dim update As String = "Update Users Set [password]='" & encrypt.Encrypt(txtNewPassword.Text) & "' Where username='" & frmMain.UserName & "'"
        dbCommand = New OleDbCommand(update, dbConn)
        Try
            dbCommand.ExecuteNonQuery()
            lblError.Text = "Đã thay đổi Password thành công!"
            txtOldPassword.Text = ""
            txtNewPassword.Text = ""
            txtVerifyPassword.Text = ""
            dbConn.Close()
        Catch ex As Exception
            dbConn.Close()
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class